<?

  parse_str($argv[1], $_GET);
  # print_r($_GET);

  if (isset($_GET['input'])) {
      $input = $_GET['input'];
  }

  $refresh = "";
  if (isset($_GET['refresh'])) {
      $refresh = "checked";
  }
  $filter = "";
  if (isset($_GET['filter'])) {
      $filter = "checked";
  }

  $logfile       = '/var/log/syslog';
  $logstat       = array();
  $lines         = array();
  $linecount     = 0;
  $linecountex   = 0;
  $linecountmax  = 1024;

  $regex_exclude = '/etc/syslogmon/exclude';
  $regex_array   = array();
  if ( strlen($filter) ) {
     $regex_array = load_regex_file($regex_exclude);
  }

  if ( file_exists($logfile) ) {
     $logfh = fopen($logfile,"r");
     $logstat = fstat( $logfh );
     $logsize = $logstat[7];
     $logsize = bytesToSize1000($logsize);
     $logmtime = strftime("%a %b %e %T",$logstat[9]);
     while ( !feof($logfh) ) {
	$line = fgets($logfh);
	$linecount++;
	if ( match_regex_array($line,$regex_array) ) {
	   $linecountex++;
	   continue;
	}
        if ( strlen($input) ) { 
           if ( ! eregi("$input",$line) ) {
	      continue;
	   }
	}
	$line = rtrim($line);
	if ( strlen($line) > 1 ) {
	    if ( $linecount > $linecountmax ) {
	        array_pop($lines);
	    }
            array_unshift($lines,$line);
        }
     }
     fclose($logfh);
  }

  # print "Lines: $linecount\n";
  # print_r($lines);


?>

<STYLE TYPE="text/css">
<!--
TD { padding: 0; spacing: 0: height: 0; font-family: monospace;}
TR { padding: 0; spacing: 0: height: 0; overflow: hidden; }
# td.odd  { background: #eef; }
# td.even { background: #ccf; }
th      { background: #b8d0f8; }
td.odd  { background: #f0f0f0; }
td.even { background: #ffffff; }
--->
</STYLE>

<P>
<CENTER>
<table width='100%' border=0 cellspacing=0 cellpadding=0>
  <tr bgcolor='#bbbbbb' align='center'>
   <form action="<?=$path;?>" method="get">
    <p>
      <input type="text" name="input" value="<?=$input;?>"/>
      <input type="checkbox" name="refresh" <? echo "$refresh"; ?> >Refresh
      <input type="checkbox" name="filter"  <? echo "$filter" ; ?> >Filter
      <input type="submit" value="grep" />
      <input type="submit" value="Acknowledge" name="action"/>
      <input type="submit" value="Archive"     name="action"/>
      <input type="submit" value="Truncate"    name="action"/>
      <input type="submit" value="Edit Filter" name="action"/>
    </p>
   </form>
 </tr>
</table>
</P>
</CENTER>

<?
  $style = array("odd","even");

  $to   = substr($lines[0],0,15);
  $from = substr($lines[count($lines)-1],0,15);
  
  print "<table align='left' width='90%' border=0 cellspacing=0 cellpadding=0>\n";
  print "<th>$logfile,  Size: $logsize,  From: $from,  To: $to,  Last Modified: $logmtime</th>\n";

  $color = 1;
  foreach ($lines as $line) {
    # $color = $color ^ 1;
    print " <tr>\n  <td class='$style[$color]'>$line</td>\n </tr>\n";
  }
  if ( $color != 0 ) {
    $color = $color ^ 1;
    print " <tr>\n  <td class='$style[$color]'><HR></td>\n </tr>\n";
  }

  $regex_count = count($regex_array);
  print " <tr>\n  <td class='$style[1]'>Lines: $linecount, Excluded: $linecountex, Regexs: $regex_count, Max lines: $linecountmax</td>\n </tr>\n";
  print "</table>\n";


function load_regex_file($file) {
    $regex_array = array();

    if (! file_exists($file))
        return $regex_array;

    $fileh = fopen($file, "r");
    while ( !feof($fileh) ) {
        $line = fgets($fileh);
    $line = rtrim($line);
    $line = ltrim($line);
    if ( strlen($line) < 1 )    { continue; }
        if ( eregi('^#.*', $line) ) { continue; }
        array_push($regex_array,$line);
    }
    fclose($fileh);

    return $regex_array;
}


function match_regex_array($string,$regex_array) {

  foreach($regex_array as $pattern) {
	if ( empty($pattern) ) { continue; }
        if ( eregi("$pattern", $string) ) {
           return 1;
           # echo "<-- CHECKING $pattern *** $string -->\n";
           # echo "<-- MATCH -->\n";
        }
    }

    return 0;

}

?>
